package chapter1.String.task;

/*
1:输入两个字符串，查找第一个字符串在第二个字符串中出现的次数
比如输入dog和 This is a good dog.
则出现了一次。
 */
public class Task1 {
    public static void main(String[] args) {
        String a = "cat";
        String b = "This is cat and cat and dog and cat cat";
        System.out.println("第一个字符串在第二个字符串中出现：" + getcount(a, b) + "次");


    }

    public static int getcount(String a, String b) {
        int index = 0, count = 0;
        while ((index = b.indexOf(a)) != -1) {
            b = b.substring(index + a.length());//截取未被查找到目标字符串的字符串
            count++;
        }
        return count;
    }
}
